package com.captjack.wechat.cp.configuration;

import com.captjack.wechat.cp.handler.LocationHandler;
import com.captjack.wechat.cp.handler.LogHandler;
import com.captjack.wechat.cp.handler.MenuHandler;
import com.captjack.wechat.cp.handler.MsgHandler;
import com.captjack.wechat.cp.handler.NullHandler;
import com.captjack.wechat.cp.handler.SubscribeHandler;
import com.captjack.wechat.cp.handler.UnsubscribeHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description description.....
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/6/10 17:00
 * package com.captjack.wechat.cache.configuration.wechat
 */
@Configuration
@ConditionalOnClass(WxCpService.class)
@EnableConfigurationProperties(CorporationWeChatProperties.class)
public class CorporationWeChatConfiguration {

    /**
     *
     */
    private final LogHandler logHandler;

    /**
     *
     */
    private final NullHandler nullHandler;

    /**
     *
     */
    private final CorporationWeChatProperties properties;

    /**
     *
     */
    private final LocationHandler locationHandler;

    /**
     *
     */
    private final MenuHandler menuHandler;

    /**
     *
     */
    private final MsgHandler msgHandler;

    /**
     *
     */
    private final UnsubscribeHandler unsubscribeHandler;

    /**
     *
     */
    private final SubscribeHandler subscribeHandler;

    @Bean
    @ConditionalOnMissingBean
    public WxCpConfigStorage configStorage() {
        WxCpInMemoryConfigStorage configStorage = new WxCpInMemoryConfigStorage();
        configStorage.setCorpId(this.properties.getCorpId());
        configStorage.setAgentId(this.properties.getAgentId());
        configStorage.setCorpSecret(this.properties.getSecret());
        configStorage.setToken(this.properties.getToken());
        configStorage.setAesKey(this.properties.getAesKey());
        return configStorage;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxCpService wxCpService(WxCpConfigStorage configStorage) {
        WxCpService service = new me.chanjar.weixin.cp.api.impl.WxCpServiceImpl();
        service.setWxCpConfigStorage(configStorage);
        return service;
    }

    @Bean
    public WxCpMessageRouter router(WxCpService wxCpService) {
        final WxCpMessageRouter newRouter = new WxCpMessageRouter(wxCpService);

        // 记录所有事件的日志 （异步执行）
        newRouter.rule().handler(this.logHandler).next();

        // 自定义菜单事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(menuHandler).end();

        // 点击菜单连接事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.VIEW).handler(this.nullHandler).end();

        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler)
                .end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unsubscribeHandler).end();

        // 上报地理位置事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.LOCATION).handler(locationHandler)
                .end();

        // 接收地理位置消息
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.LOCATION)
                .handler(locationHandler).end();

        // 扫码事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SCAN).handler(null).end();

        // 默认
        newRouter.rule().async(false).handler(msgHandler).end();

        return newRouter;
    }

    @Autowired
    public CorporationWeChatConfiguration(LogHandler logHandler, NullHandler nullHandler, CorporationWeChatProperties properties, LocationHandler locationHandler, MenuHandler menuHandler, MsgHandler msgHandler, UnsubscribeHandler unsubscribeHandler, SubscribeHandler subscribeHandler) {
        this.logHandler = logHandler;
        this.nullHandler = nullHandler;
        this.properties = properties;
        this.locationHandler = locationHandler;
        this.menuHandler = menuHandler;
        this.msgHandler = msgHandler;
        this.unsubscribeHandler = unsubscribeHandler;
        this.subscribeHandler = subscribeHandler;
    }

}
